<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;

class Star extends Controller
{
    /**
     * 打开收藏界面
     * bname 书名关键字（搜索框）
     */
    public function star(Request $request)
    {
        //验证登录
         if(empty(session('email'))){
            $this->error('请先登录!','login/login');
        }
        
        //找出所有的收藏项
        $data=Db::table('vstar')->where('email',session('email'))->select();
    
       /* $data=Db::execute("Select cartID,cart.flowerID,pictures,email,num,fname,price,yourprice
                            from cart,flower
		                    where flower.flowerID=cart.flowerID and email='".session('email')."' ");
        */
        
        //筛选条件
        $bname = $request->param('bname');
        $searchstr = 'true ';
        if (!empty($bname)){
            $searchstr.=" and bname like '%".$bname."%'";
            session('bname',$bname);
        }
        else
        {
            session('bname',null);
        }
        $data = Db::table('vstar')->where($searchstr)->order('starID','asc')->select();
    
        $this->assign('result',$data);
        return $this->fetch();
    }
    
    //添加收藏
    public function addStar(){
        $param = input('get.');
        if(empty(session('email'))){
            $this->error('请先登录!','login/login');
        }
        if(empty($param['bookID'])){
            $this->error('请选择商品！','index/index');
        }
    
        $data = Db::table('star')->where('email',session('email'))->where('bookID',$param['bookID'])->find();
    
        //$data = Db::table('star')->where("email='".session('email')."' and flowerID='".$param['flowerID']."'")->find();
        if(empty($data)){
            $result = Db::execute("insert into star(starID,email,bookID) values(null,'" . session('email') . "','" .$param['bookID']. "')");
            //dump($result);
        }
        $this->redirect(url('star/star'));
    }
    
    //删除收藏
    public function deleteStar(){
        $param = input('get.');
        if(empty(session('email'))){
            $this->error('请先登录!','login/login');
        }
        if(empty($param['bookID'])){
            $this->error('请选择商品！','index/index');
        }
        
        $data = Db::table('star')->where('email',session('email'))->where('bookID',$param['bookID'])->find();
        if(!empty($data)){
            $result=Db::execute("delete from star where email='" .session('email'). "' and bookID='" . $param['bookID'] . "'");
        }
        
        // $this->redirect(url('star/star'));
        if($result) return "ok";
        return "error";
    }
    
    //判断是否已收藏
    public function isStar()
    {
        $param = input('post.');
        $data = Db::table('star')->where('email',session('email'))->where('bookID',$param['bookID'])->find();
        
        //如果非空 即已收藏
        if(!empty($data)){
            return 'success';
        }
        
    }
}